<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\Cert;
use App\Models\CertDown;
use App\Services\CertService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class CertController extends Controller
{
    protected $CertService;
    public function __construct(CertService $CertService)
    {
        $this->CertService = $CertService;
    }

    public function index(Request $Request) {
        $search = $Request->search;
        $patent_owner = $Request->patent_owner;
        $warning = $Request->warning ?? 0;
        $type = $Request->type ?? 0;
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;

        $sql = $this->CertService->get($search);
        $sql->whereIn('type',explode(',',$type));
        if($type != 1){
            $sql->with('user');
        }
        $sql->when($patent_owner, function ($query, $patent_owner) {
            return $query->where('patent_owner','like', '%'.$patent_owner.'%');
        });
        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->orderBy('created_at','desc')->get();
        $time = time();
        $warning = [
            'info'=>0,
            'warning'=>0,
            'danger'=>0,
        ];
        foreach ($data as $k => $v) {
            $data[$k]->warning = 0;
            if($v->date_type == 0){
                if(strtotime($v->end_date) - $time > (3600 * 24 * 90) && strtotime($v->end_date) - $time < (3600 * 24 * 180)){
                    $data[$k]->warning = 3;
                    $data[$k]->warning_type = 'primary';
                    $warning['info'] ++;
                }
                if(strtotime($v->end_date) - $time > (3600 * 24 * 30) && strtotime($v->end_date) - $time < (3600 * 24 * 90)){
                    $data[$k]->warning = 2;
                    $data[$k]->warning_type = 'warning';
                    $warning['warning'] ++;
                }
                if(strtotime($v->end_date) - $time < (3600 * 24 * 30)){
                    $data[$k]->warning = 1;
                    $data[$k]->warning_type = 'danger';
                    $warning['danger'] ++;
                }
            }
        }
        return response()->json(['code' => '200', 'msg' => '操作成功','total'=>$total,'warning'=>$warning,'data'=>$data]);
    }

    public function all(Request $Request) {
        $sql = $this->CertService->get('');
        $sql->with('user')->select('id','title');
        $data = $sql->orderBy('created_at','desc')->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }


    public function show(Request $Request) {
        if(!$Request->id){
            return response()->json(['code' => '403', 'msg' => '参数错误']);
        }
        $data = Cert::where('id',$Request->id)->first();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    /**
     * 存储一个新数据。
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $Request)
    {
        $POSTONLY = Cert::POSTONLY;
        $input = $Request->only($POSTONLY);
        $data = $this->CertService->insert($input,$Request->user_id);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }


    public function update(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }

        $POSTONLY = Cert::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['id'] = $Request->id;
        $data = $this->CertService->update($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }


    public function down(Request $Request) {
        if(!$Request->id){
            return response()->json(['code' => '403', 'msg' => '参数错误']);
        }
        $data = CertDown::with('cert')->with('project')->with('user')->where('id',$Request->id)->first();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function storeDown(Request $Request)
    {
        $POSTONLY = CertDown::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['user_id'] = Auth::user()->id;
        $data = $this->CertService->storeDown($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function delete(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }
        $data = Cert::find($Request->id);
        $data->delete();
        return response()->json(['code' => '200', 'msg' => '操作成功']);
    }
}
